﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using System;
using System.IO;

namespace RotovapPlatform.Infrastructure.Common.Data;

/// <summary>
/// 实验数据库上下文工厂
/// 用于EF Core迁移时创建DbContext实例
/// </summary>
public class DesignTimeExperimentDbContextFactory : IDesignTimeDbContextFactory<ExperimentDbContext>
{
	public ExperimentDbContext CreateDbContext(string[] args)
	{
		var optionsBuilder = new DbContextOptionsBuilder<ExperimentDbContext>();
		var solutionDir = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\..\\..\\..\\.."));
		var infrastructureDir = Path.Combine(solutionDir, "src", "RotovapPlatform.Infrastructure");
		var folder = Path.Combine(infrastructureDir, "Data");

		Console.WriteLine($"Looking for database in: {folder}");
		if (!Directory.Exists(folder))
			Directory.CreateDirectory(folder);

		var dbPath = Path.Combine(folder, "Rotovap.db");
		Console.WriteLine($"Database file should be at: {dbPath}");
		optionsBuilder.UseSqlite($"Data Source={dbPath}");

		return new ExperimentDbContext(optionsBuilder.Options);
	}
}